// components_helper.cpp
// AppdicExtract / Makehfile
// AppdicExtract extracts APPDIC definition into extract.<applnbr> file / Makehfile creates defns<applnbr>.h file 
// Copyright (C) 2003, 2004, 2005 eXegeSys, Inc.
// Copyright (C) 2008 Bruce A. James
//
// This program is part of the Protea Project.
//
// The Protea Project is free software; you can redistribute it and/or modify it 
// under the terms of the GNU General Public License as published by the 
// Free Software Foundation, either version 3 of the License, or (at your 
// option) any later version.
//
// The Protea Project is distributed in the hope that it will be useful, but 
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
// for more details.
// 
// You should have received a copy of the GNU General Public License along 
// with this program.  If not, see <http://www.gnu.org/licenses/>.
//
// Please send correspondence to:
//
// theproteaproject@gmail.com
//

#include "../SDK/ToolsInterface.h"
#include "../defns.h"
#include "components_helper.h"
using namespace ProteaApp1;


XCAPPDICComponents::XCAPPDICComponents() {
	extract_all = true;
	comp_fields = NULL;
	comp_count = 0;
};


void XCAPPDICComponents::LoadComponentNumbers( const char* input, XCTxn* txn, XCAppInterface* app, long application ) {
	INFUNC( APPDICComponents::Load, app );

	XCSQLGen query( app );
	XCDBStatement* stmt;
	XCCompare comp;
	char temp[ 256 ];

	// Are we extracting everything?
	extract_all = !input;

	// Create the component defn buffer and start the DB query
	XCBuffer* buf = app->CreateBuffer( FMT_COMPONENT_DEFN );
	XCField* comp_id = buf->GetField( FLD_COMPONENT_ID );
	XCField* appl_nbr = buf->GetField( FLD_APPL_NBR );
	appl_nbr->SetLong( application );

	// Always add component zero
	if (!extract_all) {
		t_comp_fields* new_field = new t_comp_fields;
		comp_id->Clear();
		new_field->comp = comp_id->Clone();
		new_field->next = comp_fields;
		comp_fields = new_field;
	};
	query.MakeSELECT( buf );

	// If no value is sent in, extract all components, otherwise, extract specific
	if (!extract_all) {
		if (atoi(input)) {
			comp_id->SetString( input );
			query.AddWHERE( 2, appl_nbr, comp_id );
		} else {
			XCField* comp_name = buf->GetField( FLD_COMPONENT_NAME );
			comp_name->SetString( input );
			query.AddWHERE( 2, appl_nbr, comp_name );
		};
	} else
		query.AddWHERE( 1, appl_nbr );

	// Execute the statement and list the components
	stmt = txn->ExecuteSQL( &query );
	while (!stmt->Eof()) {
		comp_count++;

		OnComponentLoad( buf );

		if (!extract_all) {
			// Add to our list of components to extract
			t_comp_fields* new_field = new t_comp_fields;
			new_field->comp = comp_id->Clone();
			new_field->next = comp_fields;
			comp_fields = new_field;

			XCField* prereq = buf->GetField( FLD_COMPONENT_REQ );
			if (prereq->GetLong(true) > 0) {
				// Move on to the next component
				stmt->Release();

				query.MakeSELECT( buf );
				comp_id->MoveField( prereq );
				query.AddWHERE( 1, comp_id );
				stmt = txn->ExecuteSQL( &query );
			} else
				stmt->MoveNext();
		} else
			stmt->MoveNext();
	};
	stmt->Release();
	buf->Release();

	// If we are not extracting everything, and didn't get any components, exit
	if (!extract_all) {
		if (comp_count==0)
			RAISETOOLSERROR( ERR_NO_COMPONENTS );
	};

	OUTFUNC();
};
